<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
      <html>
        <xsl:text>
		<!--Author: Piyush Garg-->
		</xsl:text>
		<xsl:text>
        <!--Student ID: S32218012-->
		</xsl:text>
		<xsl:text>
        <!--Assignment2: Document Markup Language-->
        </xsl:text>
        <head>
          <title>
            <xsl:value-of select="concat('Bilingual Lexicon ',Dictionary/from,'-',Dictionary/to)"/>
          </title>
          <link rel="stylesheet" type="text/css" href="dictionary.css" />

        </head>
        <body>
          <h2 class="heading"><xsl:text>Browse by letter:</xsl:text></h2>
          <xsl:variable name="initialletter" select="//translation[not(@initial=preceding-sibling::translation/@initial)]/@initial" />
          <table>
            <tr>
              <xsl:for-each select="$initialletter">
                <xsl:sort select="."/>
                
                <td>
                  <a href="#{.}">
                    <xsl:value-of select ="."/>
                  </a>
                </td>
              </xsl:for-each>
            </tr>
          </table>
          <xsl:for-each select="$initialletter">
            <xsl:sort select="."/>
            <xsl:variable name="seperateinitalLetter" select="."/>
            <p>
              <a name="{.}">
                <b><xsl:value-of select="."/></b>
              </a>
            </p>
            <table>
              <xsl:for-each select="/Dictionary/translation[@initial = $seperateinitalLetter]">
                <xsl:sort select="search"/>
                <tr>
                  <td width="250" class="searchterm">
                    <xsl:value-of select="search"/>
                  </td>

                  <td class="replaceterms">
                    <xsl:choose>
                      <xsl:when test="count(replace) > 1">
                        <xsl:for-each select="replace">
                          <xsl:sort select="replace"/>
                          <xsl:variable name="replaceList" select="."/>
                          <xsl:choose>
                            <xsl:when test="position()=1">
                              <xsl:value-of select="$replaceList"/>
                            </xsl:when>
                            <xsl:otherwise>
                              <xsl:value-of select="concat(';',$replaceList)"/>
                            </xsl:otherwise>
                          </xsl:choose>
                        </xsl:for-each>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="replace"/>
                      </xsl:otherwise>
                    </xsl:choose>
                      
                  </td>
                </tr>
              </xsl:for-each>
            </table>
          </xsl:for-each>
          
          
        </body>
        
      </html>
    </xsl:template>
</xsl:stylesheet>
